package com.jsy.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

@Configuration
public class RabbitConfig {
    @Autowired
    private Environment env;
    //order queue
    @Bean("orderQueue")
    public Queue queue(){
        return QueueBuilder.durable(env.getProperty("mq.pay.queue.order")).withArgument("x-queue-type","classic").build();
    }
    //exchange
    @Bean("orderExchange")
    public Exchange exchange(){
        return ExchangeBuilder.directExchange(env.getProperty("mq.pay.exchange.order")).build();
    }
    //Binding order
    @Bean("orderExchange")
    public Binding binding(@Qualifier("orderQueue") Queue queue,@Qualifier("orderExchange")  Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(env.getProperty("mq.pay.routing.key")).noargs();
    }
    //seckillOrder queue
    @Bean("seckillOrderQueue")
    public Queue seckillOrderQueue(){
        return QueueBuilder.durable(env.getProperty("mq.pay.queue.seckillorder")).build();
    }
    //Binding seckill queue
    @Bean("orderExchange")
    public Binding seckillOrderBinding(@Qualifier("seckillOrderQueue") Queue queue,@Qualifier("orderExchange")  Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(env.getProperty("mq.pay.routing.seckillkey")).noargs();
    }

}
